<?php
namespace app\admin\controller;

use think\Config;
use app\common\controller\AdminBase;
use app\common\model\Csuser as CsuserModel;
use app\common\model\Kefu as KefuModel;
use app\common\model\AuthorWxUser as AuthorWxUserModel;
// 常用回复
use app\common\model\Csuserwords as CsuserwordsModel;
// 通用标签 au_id = 0
use app\common\model\UserGroup as UGroupModel;

class Kefu extends AdminBase
{
    protected $kefu_model;
    protected $csuser_model;
    protected $author_wx_user_model;
    protected $csuserwords_model;
    protected $_tags_ugroup_model;
    protected function _initialize(){
        parent::_initialize();
        $this->csuser_model = new CsuserModel;
        $this->kefu_model = new KefuModel;
        $this->author_wx_user_model = new AuthorWxUserModel;
        $this->csuserwords_model = new CsuserwordsModel();
        $this->_tags_ugroup_model = new UGroupModel();
    }
    public function index() {
        $kefu_list = $this->kefu_model->field('id,username,nickname,create_time,status,last_login_time,last_login_ip')->select();
        $this->assign('kefu_list',$kefu_list);
        return $this->fetch();
    }

    public function add() {
        return $this->fetch();
    }

    public function save() {
        if($this->request->isPost()) {
            $data = $this->request->post();
            $validate_result = $this->validate($data , 'kefu');
            if($validate_result !== true){
                $this->error($validate_result);
            } else {
                $data['password'] = md5($data['password'] . Config::get('salt'));
                if ( $this->kefu_model->allowField(true)->save($data)) {
                    $this->success('保存成功');
                } else {
                    $this->error('保存失败');
                }
            }
        }
    }

    public function edit($id) {
        $kefu = $this->kefu_model->where('id',$id)->find();
        $this->assign('kefu',$kefu);
        return $this->fetch();
    }

    public function update() {
        if($this->request->isPost()) {
            $data = $this->request->post();
            $validate_result = $this->validate($data , 'kefu');
            if($validate_result !== true){
                $this->error($validate_result);
            } else {
                $kefu = $this->kefu_model->find($data['id']);
                $kefu->id       = $data['id'];
                $kefu->username = $data['username'];
                $kefu->status   = $data['status'];
                $kefu->nickname   = $data['nickname'];

                if (!empty($data['password']) && !empty($data['confirm_password'])) {
                    $kefu->password = md5($data['password'] . Config::get('salt'));
                }

                if ($kefu->save() !== false) {
                    $this->success('修改成功');
                } else {
                    $this->error('修改失败');
                }
            }
        }
    }

    public function delete($id) {
        if($this->kefu_model->where('id',$id)->delete()){
            $this->success('删除成功');
        }else{
            $this->error('删除失败');
        }
    }

    
    /**
     * 授权
     * @param $id
     * @return mixed
     */
    public function auth($id)
    {
        return $this->fetch('auth', ['id' => $id]);
    }


    /**
     * AJAX获取规则数据
     * @param $id
     * @return mixed
     */
    public function getJson($id)
    {
        $wx_user = $this->author_wx_user_model->field('id,wxname')->where('authorized',1)->select();
        $auth_group_data = $this->kefu_model->field('id,f_ids')->find($id)->toArray();
        $auth_rules      = explode(',', $auth_group_data['f_ids']);
        $csuser_list  = $this->csuser_model->field('id,auid,pid,nickname')->select();
        $auth_rule_list = array_merge($wx_user,$csuser_list);

        foreach($auth_rule_list as $key => $value){
            isset($value['auid']) ? : $auth_rule_list[$key]['auid'] = 0;
            isset($value['wxname']) ?  $auth_rule_list[$key]['title'] = $value['wxname'] : null;
            isset($value['nickname']) ?  $auth_rule_list[$key]['title'] = $value['nickname'] : null;
            // $wx_user[$key]['title'] = $wx_user[$key]['wxname'];
            in_array($value['id'], $auth_rules) && $auth_rule_list[$key]['checked'] = true;
        }

        return $auth_rule_list;
    }

    /**
     * 更新权限组规则
     * @param $id
     * @param $auth_rule_ids
     */
    public function updateAuthGroupRule($id, $auth_rule_ids = '')
    {
        if ($this->request->isPost()) {
            if ($id) {
                $group_data['id']    = $id;
                $group_data['f_ids'] = is_array($auth_rule_ids) ? implode(',', $auth_rule_ids) : '';

                if ($this->kefu_model->save($group_data, $id) !== false) {
                    $this->success('授权成功');
                } else {
                    $this->error('授权失败');
                }
            }
        }
    }

    /*
     * 客服常用回复
     * */
    public function csuserws(){
        $result = $this->csuserwords_model->order('sort DESC')->select();
        $this->assign('result',$result);
        return $this->fetch();
    }

    /*
     * 添加常用回复
     *
     * */
    public function addwords(){
        return $this->fetch();
    }

    /*
     * 保存常用回复内容
     * */
    public function savewords(){
        if($this->request->isPost()) {
            $data = $this->request->post();
            $data['create_time'] = time();
            $result = $this->csuserwords_model->save($data);
            if($result){
                $this->success('保存成功');
            } else {
                $this->error('保存失败');
            }
        }
    }

    /*
     * 编辑回复内容
     * */
    public function editwords($id){
        if(!empty($id)){
            $words = $this->csuserwords_model->where('id',$id)->find();
            $this->assign('result',$words);
            return $this->fetch();
        }
    }

    /*
     * 更新回复内容
     * */
    public function updatewords($id){
        if(!empty($id)){
            $data            = $this->request->param();
//            $result = $this->csuserwords_model->where('id',$id)->update($data);
            if ($this->csuserwords_model->allowField(true)->save($data, $id) !== false) {
                $this->success('更新成功');
            } else {
                $this->error('更新失败');
            }
        }
    }

    /*
     * 公用的标签
     * 显示所有的标签
     * */
    public function common_tags($page = 1){
        $field = 'id,auid, group_id, name, sort, pid, remark, color';
        // 显示公用的标签
        // $article_list  = $this->article_model->field($field)->where($map)->order(['publish_time' => 'DESC'])->paginate(15, false, ['page' => $page]);
        $tags_list = $this->_tags_ugroup_model->field($field)->order(['sort' => 'DESC'])->paginate(15, false, ['page' => $page]);
//        var_dump($tags_list);
        return json_encode(['status' => 1, 'msg' => $tags_list]);
    }

    /*
     *
     * */




}